<?php

/**
 * 会员转移列表
 * File MembertransferController.php
 */
class MembertransferController extends AdminBase {

    public function actionIndex() {
        $objModel = new MembersTransfer('search');
        $objModel->resetAttributes();

        $arrFormData['keyword'] = isset($_GET['keyword']) ? $_GET['keyword'] : '';
        $arrFormData['type'] = isset($_GET['type']) ? $_GET['type'] : '';
        $this->assign('formData', $arrFormData);
        if (isset($_GET['MembersTransfer'])) {
            switch ($arrFormData['type']) {
                case 1:
                    $_GET['MembersTransfer']['username'] = $arrFormData['keyword'];
                    break;
                case 2:
                    $_GET['MembersTransfer']['uid'] = $arrFormData['keyword'];
                    break;
            }
            $objModel->attributes = $_GET['MembersTransfer'];
        }
        $objCriteria = $objModel->createSearchCriteria();

        //使用后台分页 
        $intCount = MembersTransfer::model()->count($objCriteria);
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;
        $page = self::page($intCount, $limit, $pageId);
        $strPage = $page->show();
        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;

        //排序
        $objSort = new CSort('MembersTransfer');
        $objSort->defaultOrder = 'status ASC,add_time DESC';
        $objSort->applyOrder($objCriteria);

        $objModels = MembersTransfer::model()->findAll($objCriteria);

        $this->assign('pages', $strPage);
        $this->assign('model', $objModel);
        $this->assign('count', $intCount);
        $this->assign('models', $objModels);

        $this->render('index');
    }

    public function actionEditStatus() {
        $uid = Yii::app()->request->getParam('uid');
        $status = Yii::app()->request->getParam('status');
        if (false && is_array($uid)) {//批量处理
            $arrUid = $uid;
            $uid = implode(',', $uid);
            $bloA = MembersTransfer::model()->updateAll(array('status' => 1), 'status = 0 AND uid in (' . $uid . ')');
            $bloB = MembersModel::model()->updateAll(array('kind' => 0, 'experience' => 0, 'score' => 0), 'kind = 1 AND uid in (' . $uid . ')');
            if ($bloA && $bloB) {
                foreach ($arrUid as $uid) {
                    MembersActionModel::model()->addAction($uid, MembersActionModel::action_system, '买家转为商家,积分清零');
                }
                $this->success('处理成功');
            } else {
                $this->success('处理失败');
            }
        } else {//单个处理
            $objModel = MembersTransfer::model()->findByPk($uid);
            if (!$objModel) {
                $this->error('找不到记录');
            }
            if ($objModel->status) {
                $this->error('已经处理过');
            }
            $objMemberModel = MembersModel::model()->findByPk($uid);
            //拒绝
            if ($status == 2) {
                $notice = Yii::app()->request->getParam('notice');
                $content = empty($notice) ? '你的用户转换为卖家的申请被拒绝,请查看' : "你的用户转换为卖家的申请被拒绝,{$notice}";
                $title = "用户买家转商家的申请";
                if (!empty($objMemberModel->email)) {
                    $mail = Yii::createComponent('application.extensions.mailer.EMailer');
                    $mail->sendEmail($objMemberModel->email, $title, $content);
                }
                if (!empty($objMemberModel->phone)) {
                    $moblie = $objMemberModel->phone;
                    Sns::getInstance()->send($moblie, $content);
                }
                $objModel->status = 2;
                $objModel->save(false, array('status'));
                //站内通知
                $extendParams = array(
                    'title' => '申请账号类型转换处理结果通知',
                    'content' => $content,
                );
                MessageModel::model()->systemNotice($objMemberModel->uid, $extendParams);
                $this->success('处理成功');
                exit;
            }

            //改经验跟积分
            if ($objModel->updateScoreExperience($objMemberModel)) {
                //站内通知
                $extendParams = array(
                    'title' => '申请账号类型转换处理结果通知',
                    'content' => "您申请的用户转换为卖家的申请已经被管理员审核通过，同时经验值和积分将被重置清零！",
                );
                MessageModel::model()->systemNotice($objMemberModel->uid, $extendParams);
                $this->success('处理成功');
            } else {
                $this->error('处理失败');
            }
        }
    }

}
